#include <iostream>
#include <thread>
#include <mutex>
static std::mutex mutex;
static int a = 0;
namespace zhou {
	template<typename T>
	class lock_guard {
	public:
		explicit lock_guard(T& mutex1) : mutex2(mutex1) {
			mutex2.lock();
		}
		~lock_guard() {
			mutex2.unlock();
		}
	private:
		T& mutex2;
	};
};
void threadF()
{
	for (int i = 0; i < 100000; ++i) {
		//mutex.lock();
		{
			//std::lock_guard<std::mutex> lck(mutex);
            //使用lock_guard可以解决 1.在临界区有代码抛出异常
            //2. 在临界区return的情况下无法解锁
            //以上情况会造成死锁
			zhou::lock_guard<std::mutex> lck(mutex);
			a++;
			a--;
		}
		//mutex.unlock();
	}
}
int main()
{
	
	std::thread t1(threadF);
	std::thread t2(threadF);
	std::cout << a << std::endl;
	t1.join();
	t2.join();

	return 0;
}



